﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
/*
 * i1.做一个背单词程序，使用数据库存储英文单词和中文词义。
 * 在Winform界面上依次显示英文单词的中文词义，
 * 用户可在编辑框中输入对应的英文单词，输入完毕后回车可与对应的英文单词进行比较，
 * 一致则显示"正确”，否则显示“错误”。
 * */
namespace i1
{
    public partial class Form1 : Form
    {
        private int WordId { get; set; }
        private string EnglishWord { get; set; }
        private string ChineseWord { get; set; }
        private int ncount { get; set; }
        private string connetStr = "server=127.0.0.1;port=3306;user=root;password=密码; database=translation;";
        public Form1()
        {
            InitializeComponent();
        }
        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {
                this.button3_Click(sender, e);//触发button事件  
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            string[] meanings = ChineseWord.Trim().Split(',');
            foreach (string s in meanings)
            {
                if(textBox1.Text.Trim()==s)
                {
                    label5.Text = "正确";
                    return;
                }
            }

            label5.Text = "错误";
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //addWords();
            WordId = 1;
            db_read();
            getCount();
            label3.Text = EnglishWord;
            

        }
        private void addWords()
        {
            String file = @"F:\cs_code\i1\words.txt";
            string line;
            string[] words;
            StreamReader sr = new StreamReader(file,Encoding.UTF8);
            MySqlConnection conn = new MySqlConnection(connetStr);
            int id = 1;
            string English = "tree";
            string Chinese = "树";
            string query;
            MySqlCommand cmd = null;
            conn.Open();
            while ((line = sr.ReadLine()) != null)
            {
                words = line.Split(' ');
                English = words[0];
                Chinese = words[1];
                query = $"insert into words(id,English,Chinese) values ({id},'{English}','{Chinese}') ";
                cmd = new MySqlCommand(query, conn);
                cmd.ExecuteNonQuery();
                id++;
            }

            
            conn.Close();
        }
        private void db_read()
        {
            MySqlConnection conn = new MySqlConnection(connetStr);
            MySqlCommand cmd = new MySqlCommand($"select* from words where id={WordId}", conn);

            conn.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            EnglishWord = dr.GetString("English");
            ChineseWord = dr.GetString("Chinese");
            conn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WordId++;
            if(WordId>ncount)
            {
                MessageBox.Show("单词背完了"); 
               // MessageBox messageBox = new MessageBox();
                WordId = 1;
            }
            db_read();
            label3.Text=EnglishWord;
        }
        private void getCount()
        {
            MySqlConnection conn = new MySqlConnection(connetStr);
            MySqlCommand cmd = new MySqlCommand($"select count(*) from words", conn);

            conn.Open();

            Object result = cmd.ExecuteScalar();//执行查询，并返回查询结果集中第一行的第一列。所有其他的列和行将被忽略。select语句无记录返回时，ExecuteScalar()返回NULL值
            if (result != null)
            {
                ncount = int.Parse(result.ToString());
            }
            conn.Close();
        }
    }
}
